// pages/termActivity/termActivity.js
const common = require('../../utils/common.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempData: {
      hasUserInfo: 1,
      hasWeChatRun: 1,
      hasToken: true,
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      myStep:"",
      step:"",
      isUserIn: true,
      notePaper: ""
    },
    userInfo: {},

    title:"",
    des:"",
    competitionPeople: "",
    startTime:0,
    endTime:0,
    conditions:"",
    winner:0,
    prize:"",
    getType:"",
  },

  // 初始化完成后每个页面会执行的函数
  initPageData() {
    let _this = this;
    _this.getArticleRank();

    wx.showLoading({
      title: '正在加载',
      mask:true,
    });
    // 获取活动说明信息
    wx.request({
      url: app.getUrl().getSystemConfigUrl,
      data:{token:app.data.token},
      method:"GET",
      success(_data){
        console.log(_data);

        switch (_data.data.message) {
          case "OK":
          case "ok":
            _this.setData({
              title: _data.data.info.semester.title,
              des: _data.data.info.semester.des,
              competitionPeople: _data.data.info.semester.competitionPeople,
              startTime: common.dateFtt("yyyy年MM月dd日",new Date(_data.data.info.semester.startTime*1000)),
              endTime: common.dateFtt("yyyy年MM月dd日", new Date(_data.data.info.semester.endTime * 1000)),
              conditions: _data.data.info.semester.conditions,
              winner: typeof (_data.data.info.semester.winner) == "number" ? parseFloat(_data.data.info.semester.winner) * 100 + "%" : _data.data.info.semester.winner,
              prize: _data.data.info.semester.prize,
              getType: _data.data.info.semester.getType,
              notePaper: _data.data.info.semester.img
            });
            break;
        }
      },
      complete(){
        wx.hideLoading();
      }
    });
  },

  // 排行榜
  getArticleRank(){
    let _this = this;
    wx.request({
      url: app.getUrl().getActivityUrl,
      data: { token: app.data.token, ratio: 0.3, effective: 10000, start: Math.floor(common.getTimeFromDay(new Date(1550505600000)).startTime/1000), end: Math.floor(common.getTimeFromDay(new Date(1556640000000)).endTime)},
      method:"GET",
      success(_data){
        console.log(_data);
        switch (_data.data.message){
          case "OK":
          case "ok":
            _this.setData({
              myStep: _data.data.info.my_step,
              step: _data.data.info.step
            });
          break;
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;

    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    _this.setData({
      userInfo: userInfo
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.commonOnLoad(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})